package main

import (
	"github.com/veandco/go-sdl2/img"
	"github.com/veandco/go-sdl2/sdl"
)

const (
	W = 670
	H = 620
)

func main() {
	sdl.Init(sdl.INIT_EVERYTHING)
	defer sdl.Quit()

	window, _ := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		W, H, sdl.WINDOW_SHOWN)
	defer window.Destroy()

	renderer, _ := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	defer renderer.Destroy()
	renderer.SetDrawColor(128, 128, 128, 255)

	pngImage, _ := img.Load("../asserts/image/texture.png")
	texture, _ := renderer.CreateTextureFromSurface(pngImage)
	pngImage.Free()
	defer texture.Destroy()

	var topLeftViewPort sdl.Rect
	topLeftViewPort.X = 0
	topLeftViewPort.Y = 0
	topLeftViewPort.W = W / 2
	topLeftViewPort.H = H / 2

	var topRightViewPort sdl.Rect
	topRightViewPort.X = W / 2
	topRightViewPort.Y = 0
	topRightViewPort.W = W / 2
	topRightViewPort.H = H / 2

	var bottomViewPort sdl.Rect
	bottomViewPort.X = 0
	bottomViewPort.Y = H / 2
	bottomViewPort.W = W
	bottomViewPort.H = H / 2

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			}
		}
		renderer.Clear()

		renderer.SetViewport(&topLeftViewPort)
		renderer.Copy(texture, nil, nil)

		renderer.SetViewport(&topRightViewPort)
		renderer.Copy(texture, nil, nil)

		renderer.SetViewport(&bottomViewPort)
		renderer.Copy(texture, nil, nil)

		renderer.Present()
		sdl.Delay(16)
	}
}
